package com.sxx.mall.search.thread;

import java.util.concurrent.*;

/**
 * @Auther: sunxx
 * @Date: 2024/8/11 - 08 - 11 - 15:42
 * @Description: com.sxx.mall.search.thread
 * @version: 1.0
 */
public class CompletableFutureDemo {

    private static ThreadPoolExecutor executor = new ThreadPoolExecutor(5
            ,50
            ,10
            , TimeUnit.SECONDS
            ,new LinkedBlockingQueue<>(100)
            , Executors.defaultThreadFactory()
            ,new ThreadPoolExecutor.AbortPolicy()
    );

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        System.out.println("main -- 线程开始了...");
        // 获取CompletableFuture对象
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            System.out.println("CompletableFuture.runAsync线程开始了...");
            int i = 100/5;
            System.out.println("CompletableFuture.runAsync线程结束了...");
        },executor);
        System.out.println("main -- 线程结束了...");

        System.out.println("------------");
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("CompletableFuture.supplyAsync线程开始了...");
            int i = 100 / 50;
            System.out.println("CompletableFuture.supplyAsync线程结束了...");
            return i;
        }, executor);
        System.out.println("获取的线程的返回结果是：" + future.get() );
    }
}
